﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using Telerik.Charting;

namespace BSCMFF.Web.Pages.Reporting
{
    public partial class skuksanie_chartov : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadSurveyData(5);
        }

        private void LoadSurveyData(int formID)
        {
            BSCEntities context = new BSCEntities();

            var questions = (from q in context.Survey_Question.Include("Survey_Form").Include("Survey_Answer.Survey_Results")
                             where q.Survey_Form.ID == formID && q.ID == 5
                             group q by q.ID into g
                             select new { qID = g.Key, count = g.Count() });

            var results = (from r in context.Survey_Results.Include("Survey_Answer.Survey_Question")
                           where r.Survey_Answer.Question_FK == 21
                           group r by r.Answer_FK into ans
                           select new { pocet = ans.Count(), odpoved = ans.Key });

            foreach (var r in results)
            {
                RadChart rch1 = new RadChart();
                rch1.ID = "RadChart" + r.odpoved.ToString(); // sq.ID.ToString();
                //rch1 = "results";

                // Create a ChartSeries and assign its name and chart type
                //ChartSeries chartSeries = new ChartSeries();
                //chartSeries.Name = "Odpovede";
                //chartSeries.Type = ChartSeriesType.Pie;
                rch1.DataSource = r;
                rch1.Series[0].DataYColumn = "pocet";
                RadChart1.PlotArea.XAxis.DataLabelsColumn = "Name";
                rch1.DataBind();

                PlaceHolderCharts.Controls.Add(rch1);
            }

            //foreach(Survey_Question sq in questions)
            //{
            //Label l = new Label();
            //l.ID= "Label" + sq.ID.ToString();
            //l.Text = sq.Question;
            //PlaceHolderCharts.Controls.Add(l);

            RadChart rch = new RadChart();
            rch.ID = "RadChart1";//+ sq.ID.ToString();

            // Create a ChartSeries and assign its name and chart type
            ChartSeries chartSeries = new ChartSeries();
            chartSeries.Name = "Sales";
            chartSeries.Type = ChartSeriesType.Pie;
            // add new items to the series,
            // passing a value and a label string
            chartSeries.AddItem(120, "Internet");
            chartSeries.AddItem(140, "Retail");
            chartSeries.AddItem(35, "Wholesale");

            rch.Series.Add(chartSeries);
            PlaceHolderCharts.Controls.Add(rch);

            //rch.Series.Add();

            //}
            /*
            var results = (from r in context.Survey_Results.Include("Survey_Answer.Survey_Question.Survey_Form")
                           where r.Survey_Answer.Survey_Question.Survey_Form.ID == formID
                           group r by new { r.Survey_Answer.Question_FK, r.Survey_Answer.ID } into q
                           select new { count = q.Count(), answers = q.Key.ID, quest = q.Key.Question_FK }).ToList();

            List<string> questionsName = (from q in context.Survey_Question.Include("Survey_Form")
                                 where q.Survey_Form.ID == formID
                                 select q.Question).ToList();





            RadChart1.DataSource = results;
            RadChart1.DataGroupColumn = "Question_FK";
            RadChart1.DataBind();*/
        }
    }
}